package com.situ.staffmgr.util;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class BaseServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码格式
//				response.setContentType("text/html;charset=utf-8");
//				request.setCharacterEncoding("utf-8");
				//区分操作类型
				//获取方法名称
				String action = request.getParameter("action");
				//反射的方式 获取
				if (action!=null) {
					Class cls= this.getClass();
					//通过反射获取action同名的方法
					try {
						Method method= cls.getMethod(action, HttpServletRequest.class,HttpServletResponse.class);
						//执行method 获取返回值
						method.invoke(this, request,response);
					} catch (NoSuchMethodException e) {
						// TODO Auto-generated catch block
						response.getWriter().append("未找到方法"+action);
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						response.getWriter().append("方法获取失败"+action);
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						response.getWriter().append("方法权限问题"+action);
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						response.getWriter().append("方法参数问题"+action);
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						response.getWriter().append("方法对象问题"+action);
					}
					
				}else {
					response.getWriter().append("缺少action参数！！！");
				}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
